package com.bluesmart.mia;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;

import com.util.ImageLoaderHelper;
import com.widget.TouchImageView;

import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class ImageShowActivity extends BaseActivity {

	private ViewPager viewPager; // android-support-v4中的滑动组件
	private List<ImageView> imageViews; // 滑动的图片集合

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_image_show);

		init();
	}

	private void init() {
		imageViews = new ArrayList<ImageView>();
		viewPager = (ViewPager) findViewById(R.id.vp);
		viewPager.setOnPageChangeListener(new MyPageChangeListener());

		final OnClickListener listen = new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		};

		imageViews.clear();

		String img = getIntent().getStringExtra("imgs");
		if(img.length() > 0 && img.charAt(0) == ',') img = img.substring(1);
		String imgList[] = img.split(",");
		
//		for (int i = 0; i < imgList.length; i++) 
		{
			TouchImageView imageView = new TouchImageView(this);
			imageView.setOnClickListener(listen);
			imageView.setMaxZoom(4f);
			ImageLoaderHelper.displayImage(R.drawable.a37, imageView,imgList[getIntent().getExtras().getInt("index")]);
			imageViews.add(imageView);
		}

		viewPager.setAdapter(new MyAdapter());// 设置填充ViewPager页面的适配器
	}

	Timer mScaleTime = null;
	boolean mScale = false;

	// ==================shantu ===============

	/**
	 * 当ViewPager中页面的状态发生改变时调用
	 * 
	 * @author Administrator
	 * 
	 */
	private class MyPageChangeListener implements OnPageChangeListener {
		/**
		 * This method will be invoked when a new page becomes selected.
		 * position: Position index of the new selected page.
		 */
		public void onPageSelected(int position) {
		}

		public void onPageScrollStateChanged(int arg0) {

		}

		public void onPageScrolled(int arg0, float arg1, int arg2) {

		}
	}

	/**
	 * 填充ViewPager页面的适配器
	 * 
	 * @author Administrator
	 * 
	 */
	private class MyAdapter extends PagerAdapter {

		public MyAdapter() {
		}

		@Override
		public int getCount() {
			// return imageResId.length;
			return imageViews.size();
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			((ViewPager) arg0).addView(imageViews.get(arg1));
			return imageViews.get(arg1);
		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			((ViewPager) arg0).removeView((View) arg2);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {

		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {

		}

		@Override
		public void finishUpdate(View arg0) {

		}
	}
}
